/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ucmconfchecker;

import java.util.Date;
import ucmconfchecker.pActivationTrace.ArchEvent;
import ucmconfchecker.pActivationTrace.ResponsibilityActivationTrace;
import ucmconfchecker.pArchImpMapping.ArchImpMapping;
import ucmconfchecker.pArchitecture.Responsibility;
import ucmconfchecker.pExecutionTrace.ExecutionTrace;
import ucmconfchecker.pExecutionTrace.ImplEvent;

/**
 *
 * @author Administrador
 */
public class TraceLogReconstruction implements Reconstruction{

    public ResponsibilityActivationTrace execute(ExecutionTrace trace, ArchImpMapping mapping) {
        ExecutionTrace unused = new ExecutionTrace();
        unused.addAll(trace);
        ResponsibilityActivationTrace result =new ResponsibilityActivationTrace();
        
        for(Responsibility responsibility: mapping.keySet()){
            ExecutionTrace respTrace = mapping.get(responsibility);
            boolean valid =true;
            Date begin = null;
            Date end =null;
            for(int e =0; e<respTrace.size()&&valid;e++){
                ImplEvent event = respTrace.get(e);
                //System.out.println("RespEvent: "+ event);
                if(begin == null || begin.compareTo(event.getTimeStamp())==1)
                    begin = event.getTimeStamp();
                if(end == null || end.compareTo(event.getTimeStamp())==-1)
                    end = event.getTimeStamp();
                if(!unused.remove(event)){
                    valid=false;
                }
            }if(valid){
                //System.out.println("Activation:"+ responsibility.toString());
                ArchEvent event = new ArchEvent(responsibility, begin, end, respTrace);
                result.add(event);
//                System.out.println("event:"+ event);
            }
        }if(!unused.isEmpty()){
            Date begin = null;
            Date end =null;
            for(int e =0; e<unused.size();e++){
                ImplEvent event = unused.get(e);
                //System.out.println("RespEvent: "+ event);
                if(begin == null || begin.compareTo(event.getTimeStamp())==1)
                    begin = event.getTimeStamp();
                if(end == null || end.compareTo(event.getTimeStamp())==-1)
                    end = event.getTimeStamp();
            }
            ArchEvent event = new ArchEvent(null, begin, end, unused);
            result.add(event);
//            System.out.println("unused:"+event);
        }
        return result;
    }
    
}
